package com.dkd.manage.controller;


import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.utils.file.FileUtils;
import com.dkd.common.utils.oss.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.platform.FileStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * 通用接口
 */
@Api(value = "/common", tags = {"通用接口"})
@Slf4j
@RestController("fileCommonController")
@RequestMapping("/common")
public class CommonController {
    private final AliOssUtil aliOssUtil;
    @Autowired
    private FileStorageService fileStorageService;

    public CommonController(AliOssUtil aliOssUtil) {
        this.aliOssUtil = aliOssUtil;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
//    @PostMapping("/upload")
//    public AjaxResult upload(MultipartFile file) {
//        log.info("文件上传功能：{}",file);
//        try {
//            //原始文件名
//            String originalFilename = file.getOriginalFilename();
//            //原始文件名后缀
//            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//            //构建新文件名
//            String objectName=UUID.randomUUID().toString()+extension;
//            //文件请求路径
//            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
//            AjaxResult ajax = AjaxResult.success();
//            ajax.put("url", filePath);
//            ajax.put("fileName", filePath);
//            ajax.put("newFileName", FileUtils.getName(filePath));
//            ajax.put("originalFilename", file.getOriginalFilename());
//            return ajax;
//        } catch (IOException e) {
//            log.error("文件上传失败:{}",e.getMessage());
//        }
//        return AjaxResult.error("文件上传失败");
//    }
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", name = "file", value = "", required = true)
    })
    @ApiOperation(value = "文件上传", notes = "文件上传", httpMethod = "POST")
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws Exception {
        log.info("文件上传功能：{}",file);
        //指定oss保存文件路径，格式为：dkd-image/2020/08/01/
        String objectNamePath=LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))+ "/";
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //原始文件名后缀
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构建新文件名
        String objectName=UUID.randomUUID().toString()+extension;
        FileInfo fileInfo = fileStorageService.of(file)
                .setPath(objectNamePath)
                .upload();
        //获取上传文件的路径
        String filePath = fileInfo.getUrl();
        AjaxResult ajax = AjaxResult.success();
            ajax.put("url", filePath);
            ajax.put("fileName", filePath);
            ajax.put("newFileName", filePath);
            ajax.put("originalFilename", file.getOriginalFilename());
        return ajax;
    }
}
